package cate.game.farm.record;

import cate.game.play.proce.play.PlayPO;
import cate.game.role.Role;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@NoteClass("farm通关记录")
public abstract class FarmPassRecord {

	@NoteField("关卡")
	public int level;

	@NoteField("最快的玩家")
	public FastPassRecord fast;

	@NoteField("战力最低的玩家")
	public LowPowerPassRecord low;

	@NoteField("最早的玩家")
	public EarlyPassRecord early;


	public synchronized void tryRecord(Role role, List<PlayPO> plays) {
		Set<String> oldUids = getAllReplayUids();
		if (recordFast()) {
			if (fast == null) {
				fast = new FastPassRecord();
			}
			fast.tryRecord(role, plays);
		}
		if (recordLowPower()) {
			if (low == null) {
				low = new LowPowerPassRecord();
			}
			low.tryRecord(role, plays);
		}
		if (recordEarly()) {
			if (early == null) {
				early = new EarlyPassRecord();
			}
			early.tryRecord(role, plays);
		}
		Set<String> curUids = getAllReplayUids();

		oldUids.removeAll(curUids);
		if(!oldUids.isEmpty()){
			for (String removeUid : oldUids) {
				role.getGame().facilities.getMongo().play.deleteById(removeUid);
			}
		}
		for (PlayPO play : plays) {
			if(curUids.contains(play.uid)){
				role.getGame().facilities.getMongo().play.save(play);
			}
		}
	}


	//是否记录最快通关玩家
	protected abstract boolean recordFast();

	//是否记录战力最低的玩家
	protected abstract boolean recordLowPower();

	//是否记录最早的通关玩家
	protected abstract boolean recordEarly();


	private Set<String> getAllReplayUids() {
		Set<String> uids = new HashSet<>();
		if (fast != null) {
			uids.addAll(fast.replayUids);
		}
		if (low != null) {
			uids.addAll(low.replayUids);
		}
		if (early != null) {
			uids.addAll(early.replayUids);
		}
		return uids;
	}



}
